package com.example.xd.myijkplayer.textureview

import android.content.Context
import android.graphics.SurfaceTexture
import android.net.Uri
import android.util.AttributeSet
import android.view.Surface
import android.view.TextureView
import tv.danmaku.ijk.media.exo.IjkExoMediaPlayer
import tv.danmaku.ijk.media.player.IMediaPlayer
import tv.danmaku.ijk.media.player.IjkTimedText

/**
 * @auther XuDan on 2020/9/15.
 */
class TextureViewControl @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextureView(context, attrs, defStyleAttr), TextureView.SurfaceTextureListener,
    IMediaPlayer.OnSeekCompleteListener, IMediaPlayer.OnTimedTextListener,
    IMediaPlayer.OnVideoSizeChangedListener, IMediaPlayer.OnInfoListener,
    IMediaPlayer.OnErrorListener, IMediaPlayer.OnCompletionListener,
    IMediaPlayer.OnBufferingUpdateListener, IMediaPlayer.OnPreparedListener, ITexture {
    init {
        surfaceTextureListener = this
        initMediaplayer()
    }

    private var ijkExoMediaPlayer: IjkExoMediaPlayer? = null
    private fun initMediaplayer() {
        if (ijkExoMediaPlayer == null) {
            ijkExoMediaPlayer = IjkExoMediaPlayer(context)
            ijkExoMediaPlayer!!.setOnSeekCompleteListener(this)
            ijkExoMediaPlayer!!.setOnTimedTextListener(this)
            ijkExoMediaPlayer!!.setOnVideoSizeChangedListener(this)
            ijkExoMediaPlayer!!.setOnInfoListener(this)
            ijkExoMediaPlayer!!.setOnErrorListener(this)
            ijkExoMediaPlayer!!.setOnCompletionListener(this)
            ijkExoMediaPlayer!!.setOnBufferingUpdateListener(this)
            ijkExoMediaPlayer!!.setOnPreparedListener(this)

        }

    }

    private var isPerpared = false
    override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) {
        //SurfaceTexture准备就绪,关联mediaPlayer
        ijkExoMediaPlayer!!.setSurface(Surface(surface))
        if (!isPerpared) {
            ijkExoMediaPlayer!!.prepareAsync()
            isPerpared = true
        }


    }


    override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {
        //surfaceTexture缓冲大小变化

    }

    override fun onSurfaceTextureUpdated(surface: SurfaceTexture?) {
        //surfaceTexture通话UpdataImage更新
    }

    override fun onSurfaceTextureDestroyed(surface: SurfaceTexture?): Boolean {
        //surfaceTexture即将被销毁
        return false
    }

    override fun onSeekComplete(p0: IMediaPlayer?) {
        if (stardTextureListener != null) {
            stardTextureListener.onSeekComplete(p0)
        }
    }

    override fun onTimedText(p0: IMediaPlayer?, p1: IjkTimedText?) {
        if (stardTextureListener != null) {
            stardTextureListener.onTimedText(p0, p1)
        }
    }

    override fun onVideoSizeChanged(p0: IMediaPlayer?, p1: Int, p2: Int, p3: Int, p4: Int) {
        if (stardTextureListener != null) {
            stardTextureListener.onVideoSizeChanged(p0, p1, p2, p3, p4)
        }
    }

    override fun onInfo(p0: IMediaPlayer?, p1: Int, p2: Int): Boolean {
        if (stardTextureListener != null) {
            stardTextureListener.onInfo(p0, p1, p2)
        }
        return true
    }


    override fun onError(p0: IMediaPlayer?, p1: Int, p2: Int): Boolean {
        if (stardTextureListener != null) {
            stardTextureListener.onError(p0, p1, p2)
        }
        return true
    }


    override fun onCompletion(p0: IMediaPlayer?) {
        if (stardTextureListener != null) {
            stardTextureListener.onCompletion(p0)
        }
    }

    override fun onBufferingUpdate(p0: IMediaPlayer?, p1: Int) {
        if (stardTextureListener != null) {
            stardTextureListener.onBufferingUpdate(p0, p1)
        }
    }

    override fun onPrepared(mediaPlayer: IMediaPlayer?) {
        if (stardTextureListener != null) {
            stardTextureListener.onPrepared(mediaPlayer)
        }
    }

    override fun start() {
        ijkExoMediaPlayer!!.start()
        isPlayerPause = false
    }

    override fun restart() {
        isPlayerPause = false
        ijkExoMediaPlayer!!.start()
    }

    override fun pause() {
        ijkExoMediaPlayer!!.pause()
        isPlayerPause = true
    }

    override fun seekTo(pos: Long) {
        ijkExoMediaPlayer!!.seekTo(pos)
    }

    override fun isBufferingPlaying(): Boolean {
        TODO("Not yet implemented")
    }

    override fun isBufferingPaused(): Boolean {
        TODO("Not yet implemented")
    }

    override fun isPlaying(): Boolean {
        return ijkExoMediaPlayer!!.isPlaying
    }

    private var isPlayerPause = false
    override fun isPaused(): Boolean {
        return isPlayerPause
    }

    private var isPlayerFullScreen = false
    override fun isFullScreen(): Boolean {
        return isPlayerFullScreen
    }

    override fun enterFullScreen() {
        isPlayerFullScreen = true
    }

    override fun exitFullScreen() {
        isPlayerFullScreen = false
    }

    private var isPlayerTinyWindow = false
    override fun isTinyWindow(): Boolean {
        return isPlayerTinyWindow
    }

    override fun enterTinyWindow() {
        isPlayerTinyWindow = true
    }

    override fun exitTinyWindow() {
        isPlayerTinyWindow = false
    }

    override fun duration(): Long {
        return ijkExoMediaPlayer!!.duration
    }

    override fun currentPosition(): Long {
        return ijkExoMediaPlayer!!.currentPosition
    }

    override fun bufferPercentage(): Int {
        TODO("Not yet implemented")
    }


    override fun release() {
        ijkExoMediaPlayer!!.release()
    }

    override fun setPath(path: String) {
        ijkExoMediaPlayer!!.setDataSource(context, Uri.parse(path))
    }

    private lateinit var stardTextureListener: StardTextureListener
    fun setStadrTextureListener(stardTextureListener: StardTextureListener) {
        this.stardTextureListener = stardTextureListener
    }


}